﻿Imports System.Data.OleDb
Public Class Frm_NhanVienBanVe

    Public Sub gvSuatChieu_Load()
        Dim bus As SuatChieuBUS = New SuatChieuBUS()
        gvSuatChieu.DataSource = bus.GetTable("select * from SUATCHIEU")
    End Sub

    Public Sub gvVe_Load()
        Dim bus As VeBUS = New VeBUS()
        gvVe.DataSource = bus.GetTable("select * from VE")
    End Sub
    Public Sub gvThanhVien_Load()
        Dim bus As ThanhVienBUS = New ThanhVienBUS()
        gvThanhVien.DataSource = bus.GetTable("select * from THANHVIEN")
    End Sub

    Private Sub Frm_NhanVienBanVe_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lbUserName.Text = Frm_Login.username + " (" + Frm_Login.acctype + ")"
        gvSuatChieu_Load()
        gvThanhVien_Load()
        gvVe_Load()

        ComboBox_Load()

    End Sub

    Private Sub btnChonGhe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonGhe.Click
        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim dt As DataTable = New DataTable()
        Dim str As String = "select SoDay, SoHang from VE where MaSuatChieu=" + gvSuatChieu.CurrentRow.Cells(0).Value.ToString()
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(str, cn)
        da.Fill(dt)
        cn.Close()

        Dim frm As New frmChonGhe()

        For i = 0 To 9 Step 1
            For j = 0 To 9 Step 1
                Frm_Login.arr(i, j).sl = New Button()
                Frm_Login.arr(i, j).sl.Location = New Point(i * 50, j * 50)
                Frm_Login.arr(i, j).sl.Size = New Size(50, 50)
                Frm_Login.arr(i, j).sl.BackgroundImage = RapChieuPhim.My.Resources.Resources.seat_0
                Frm_Login.arr(i, j).sl.BackgroundImageLayout = ImageLayout.Stretch
                Frm_Login.arr(i, j).sl.Text = (j + 1).ToString() + ":" + (i + 1).ToString()
                AddHandler Frm_Login.arr(i, j).sl.Click, AddressOf frm.Button_Click
                Frm_Login.arr(i, j).st = 0
                frm.Controls.Add(Frm_Login.arr(i, j).sl)
            Next
        Next

        For i = 0 To dt.Rows.Count() - 1 Step 1
            Dim x As Integer = dt.Rows(i)(0) - 1
            Dim y As Integer = dt.Rows(i)(1) - 1
            Frm_Login.arr(x, y).sl.BackgroundImage = RapChieuPhim.My.Resources.Resources.seat_1
            Frm_Login.arr(x, y).sl.Enabled = False
            Frm_Login.arr(x, y).st = 1
        Next

        Frm_Login.banve = New VeDTO()
        Frm_Login.banve.MaSuatChieu = gvSuatChieu.CurrentRow.Cells(0).Value.ToString()
        Frm_Login.banve.GiaVeBan = gvSuatChieu.CurrentRow.Cells(7).Value.ToString()
        If cbHSSV.Checked = True Then
            Frm_Login.banve.IDThanhVien = gvThanhVien.CurrentRow.Cells(0).Value.ToString()
            Frm_Login.banve.VeDat = True
        Else
            Frm_Login.banve.IDThanhVien = "NULL"
            Frm_Login.banve.VeDat = False
        End If

        frm.ShowDialog()
        gvVe_Load()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim bus As VeBUS = New VeBUS()
        Dim tmp As VeDTO = New VeDTO()
        tmp.MaVe = lbMaVe.Text
        bus.Delete(tmp)
        MessageBox.Show("Thành công", "Thông báo")
        gvVe_Load()
    End Sub
    Private Sub ComboBox_Load()
        Dim bus As SuatChieuBUS = New SuatChieuBUS()
        cbxMaSuatChieu.DataSource = bus.GetTable("select * from SUATCHIEU")
        cbxMaSuatChieu.DisplayMember = "MaSuatChieu"
        cbxMaSuatChieu.ValueMember = "MaSuatChieu"
        Dim tv As SuatChieuBUS = New SuatChieuBUS()
        cbxThanhVien.DataSource = bus.GetTable("select * from THANHVIEN")
        cbxThanhVien.DisplayMember = "IDThanhVien"
        cbxThanhVien.ValueMember = "IDThanhVien"
    End Sub
    Private Sub gvVe_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvVe.SelectionChanged
        Dim bus As VeBUS = New VeBUS()
        Try
            If gvVe.SelectedRows.Count = 0 Then
                Return
            End If
            lbMaVe.Text = gvVe.CurrentRow.Cells(0).Value.ToString()
            txtGiaVe.Text = gvVe.CurrentRow.Cells(2).Value.ToString()
            If gvVe.CurrentRow.Cells(4).Value = True Then
                rdVeDat.Checked = True
            Else
                rdVeMua.Checked = True
            End If
            If (gvVe.CurrentRow.Cells(3).Value.ToString() <> "NULL") Then
                cbLaThanhVien.Checked = True
            Else
                cbLaThanhVien.Checked = False
            End If
            cbxThanhVien.Text = gvVe.CurrentRow.Cells(3).Value.ToString()
            txtSoDay.Text = gvVe.CurrentRow.Cells(6).Value.ToString()
            txtSoHang.Text = gvVe.CurrentRow.Cells(5).Value.ToString()

            cbxMaSuatChieu.Text = gvVe.CurrentRow.Cells(1).Value.ToString()
            Dim HSSV As ThanhVienDTO = New ThanhVienDTO()

            Dim busHSSV As ThanhVienBUS = New ThanhVienBUS()

            Dim kt As String = String.Format("select HSSV from THANHVIEN where IDThanhVien = '" & gvVe.CurrentRow.Cells(3).Value.ToString() & "'")
            Dim dt As String
            dt = bus.GetValue(kt)
            If (dt = "True") Then
                cbHSSV.Checked = True
            Else
                cbHSSV.Checked = False
            End If
        Catch
        End Try
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        If (cbxMaSuatChieu.SelectedValue.ToString() = "" Or txtGiaVe.Text = "" Or txtSoDay.Text = "" Or txtSoHang.Text = "") Then
            MessageBox.Show("Vui lòng nhập đủ thông tin", "Thông báo")
        Else
            Dim ghe As VeDTO = New VeDTO()
            Dim Ghebus As VeBUS = New VeBUS()
            Dim kt As String = String.Format("select * from VE where MaSuatChieu = " & cbxMaSuatChieu.SelectedValue.ToString() & " and SoHang = " & txtSoHang.Text & " and SoDay = " & txtSoDay.Text & " and MaVe <> " & lbMaVe.Text)
            Dim dt = New DataTable
            dt = Ghebus.GetTable(kt)
            If (dt.Rows.Count = 0) Then
                Dim bus As VeBUS = New VeBUS()
                Dim temp As VeDTO = New VeDTO()
                temp.GiaVeBan = txtGiaVe.Text
                temp.MaSuatChieu = cbxMaSuatChieu.SelectedValue.ToString()
                temp.MaVe = lbMaVe.Text
                temp.SoDay = txtSoDay.Text
                temp.SoHang = txtSoHang.Text

                If (cbLaThanhVien.Checked = True) Then
                    temp.IDThanhVien = cbxThanhVien.SelectedValue.ToString()
                Else
                    temp.IDThanhVien = "NULL"
                End If
                If (rdVeDat.Checked = True) Then
                    temp.VeDat = True
                Else
                    temp.VeDat = False
                End If

                bus.Update(temp)
                MessageBox.Show("OK", "Thông báo")
                gvVe_Load()
            Else
                MessageBox.Show("Ghế đã được đặt trước", "Thông báo")
            End If
        End If
    End Sub

    Private Sub cbxThanhVien_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxThanhVien.SelectionChangeCommitted
        Dim HSSV As ThanhVienDTO = New ThanhVienDTO()

        Dim bus As ThanhVienBUS = New ThanhVienBUS()

        Dim kt As String = String.Format("select HSSV from THANHVIEN where IDThanhVien = '" & cbxThanhVien.SelectedValue.ToString() & "'")
        Dim dt As String
        dt = bus.GetValue(kt)
        If (dt = "True") Then
            cbHSSV.Checked = True
        Else
            cbHSSV.Checked = False
        End If
    End Sub

    Private Sub btnTimVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimVe.Click
        If (rdTimMaVe.Checked = True) Then
            Dim bus As VeBUS = New VeBUS()
            Dim str As String = "select * from VE where MaVe like '%" + txtTimVe.Text + "%'"
            gvVe.DataSource = bus.GetTable(str)
        ElseIf (rdTimMaSuatChieu.Checked = True) Then
            Dim bus As VeBUS = New VeBUS()
            Dim str As String = "select * from VE where MaSuatChieu like '%" + txtTimVe.Text + "%'"
            gvVe.DataSource = bus.GetTable(str)
        ElseIf (rdTimNgayChieu.Checked = True) Then
            Dim bus As VeBUS = New VeBUS()
            Dim str As String = "select VE.MaVe, VE.MaSuatChieu, VE.GiaVeBan, VE.IDThanhVien, VE.VeDat, VE.SoHang, VE.SoDay from VE, SUATCHIEU where VE.MaSuatChieu = SUATCHIEU.MaSuatChieu and SUATCHIEU.NgayChieu like '%" + txtTimVe.Text + "%'"
            gvVe.DataSource = bus.GetTable(str)

        End If
    End Sub

    Private Sub btnTimSC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimSC.Click
        If (rdTimPhongChieu.Checked = True) Then
            Dim bus As SuatChieuBUS = New SuatChieuBUS()
            Dim str As String = "select * from SUATCHIEU where MaPhongChieu like '%" + txtTimSC.Text + "%'"
            gvSuatChieu.DataSource = bus.GetTable(str)
        ElseIf (rdTimPhim.Checked = True) Then
            Dim bus As SuatChieuBUS = New SuatChieuBUS()
            Dim str As String = "select * from SUATCHIEU where TenPhim like '%" + txtTimSC.Text + "%'"
            gvSuatChieu.DataSource = bus.GetTable(str)
        End If

    End Sub

    Private Sub btnTimTV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimTV.Click
        If (rdTimID.Checked = True) Then
            Dim bus As ThanhVienBUS = New ThanhVienBUS()
            Dim str As String = "select * from THANHVIEN where IDThanhVien like '%" + txtTimTV.Text + "%'"
            gvThanhVien.DataSource = bus.GetTable(str)
        ElseIf (rdTimTenTV.Checked = True) Then
            Dim bus As ThanhVienBUS = New ThanhVienBUS()
            Dim str As String = "select * from THANHVIEN where TenThanhVien like '%" + txtTimTV.Text + "%'"
            gvThanhVien.DataSource = bus.GetTable(str)
        ElseIf (rdTimSDT.Checked = True) Then
            Dim bus As ThanhVienBUS = New ThanhVienBUS()
            Dim str As String = "select * from THANHVIEN where SDT like '%" + txtTimTV.Text + "%'"
            gvThanhVien.DataSource = bus.GetTable(str)
        End If
    End Sub

    Private Sub lbDangXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbDangXuat.Click
        Frm_Login.txtTenDN.Text = ""
        Frm_Login.txtMatKhau.Text = ""
        Frm_Login.txtTenDN.Select()
        Close()
    End Sub

    Private Sub lbUserName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbUserName.Click

    End Sub

    Private Sub help1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles help1.Click
        Help.ShowHelp(Me, Application.StartupPath() + "\HuongDan.chm")
    End Sub
End Class